package com.cctc.rds.raft.demo;

import com.cctc.rds.raft.core.client.command.RaftCommandClient;
import com.cctc.rds.raft.core.client.command.RaftCommandResponse;
import com.cctc.rds.raft.core.log.LogService;
import com.cctc.rds.raft.core.log.commit.statemachine.StateMachine;
import com.cctc.rds.raft.core.log.impl.DefaultLogService;
import com.cctc.rds.raft.core.node.NodeIpPort;
import com.cctc.rds.raft.demo.log.commit.statemachine.impl.PrintCommandStateMachine;
import com.cctc.rds.raft.sdk.RaftStarter;

import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;

public class RaftDemoApplication {
    private static LogService logService = new DefaultLogService();
    private static StateMachine stateMachine = new PrintCommandStateMachine();
    private static Set<NodeIpPort> nodes = new HashSet<>();

    static {
        nodes.add(new NodeIpPort(1, "127.0.0.1", 9001));
        nodes.add(new NodeIpPort(2, "127.0.0.1", 9002));
        nodes.add(new NodeIpPort(3, "127.0.0.1", 9003));
    }


    public static void main(String[] args) {
        if (args.length < 1)
            throw new RuntimeException("请输入 nodeId ... ...");
        int nodeId = Integer.parseInt(args[0]);
        RaftCommandClient raftCommandClient = RaftStarter.start(nodeId, logService, stateMachine, nodes);

        byte[] command = "select * from test;".getBytes(StandardCharsets.UTF_8);
        try {
            RaftCommandResponse response = raftCommandClient.exec(command);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
